package com.mmx.baseservice.system.tools.timer.task;

import com.mmx.baseservice.common.ex.ServiceException;
import com.mmx.baseservice.common.utils.LogUtil;
import com.mmx.baseservice.common.utils.SpringContextUtil;
import com.mmx.baseservice.common.utils.UUIDUtil;
import com.mmx.baseservice.system.tools.timer.pojo.ScheduleJobEntity;
import com.mmx.baseservice.system.tools.timer.util.QuartzUtil;
import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
@DisallowConcurrentExecution
public class MarketingActivityScheduleHandle {
//
//    @Autowired
//    private SchedulerFactoryBean schedulerFactoryBean;
//    @Resource
//    private SysTimerConfigMapper sysTimerConfigMapper;
//
//    @Scheduled(fixedRate = 1000 * 60 * 60) // 每隔5s查库
//    public void scheduleRefreshCron() throws ServiceException {
//        try {
//            // 获取一个调度器
//            LogUtil.info("--------获取执行周期--------");
//            List<SysTimerConfig> sysTimerList = sysTimerConfigMapper.selectByMap(new HashMap<>());
//            Scheduler scheduler = schedulerFactoryBean.getScheduler();
//            for (SysTimerConfig sysTimer : sysTimerList) {
//                // 设置参数
//                ScheduleJobEntity scheduleJob = new ScheduleJobEntity();
//                scheduleJob.setJobName(sysTimer.getJobName());
//                scheduleJob.setJobGroup(sysTimer.getJobGroup());
//                scheduleJob.setCronExpression(sysTimer.getCron());
//                scheduleJob.setTargetObject(sysTimer.getJobClass());
//                scheduleJob.setTargetMethod("executeInternal");
//                scheduleJob.setConcurrent(true);
//                Map<String, Object> paramsMap = new HashMap<>();
//                paramsMap.put("uuid", UUIDUtil.getCode());
//
//                // 获取triggerKey
//                TriggerKey triggerKey = TriggerKey.triggerKey(scheduleJob.getJobName(), scheduleJob.getJobGroup());
//                // 获取trigger
//                CronTrigger trigger = (CronTrigger) scheduler.getTrigger(triggerKey);
//                // 不存在，创建一个
//                if (null == trigger) {
//                    // 创建一个触发器
//                    LogUtil.info(sysTimer.getJobClass());
//                    Class<? extends Job> jobClass = (Class<? extends Job>) SpringContextUtil.getApplicationContext().getBean(sysTimer.getJobClass()).getClass();
//                    trigger = QuartzUtil.createSheduler(scheduler, scheduleJob, paramsMap, jobClass);
//                } else {
//                    // Trigger已存在，那么更新相应的定时设置
//                    QuartzUtil.updateScheduler(scheduler, scheduleJob, triggerKey, trigger);
//                }
//                // 如果定时器处于暂停状态则进行暂停处理
//                if (sysTimer.getStatus() == -1) {
//                    QuartzUtil.pauseScheduler(scheduler, trigger);
//                }
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//            throw new ServiceException(e.getMessage());
//        }
//    }
}
